<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="民情直通"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="问题内容"><u-input v-model="will.title" type="text" placeholder="请输入问题内容" /></u-form-item>
					<u-form-item class="form-item" label="问题编号"><u-input v-model="will.opinionNo" type="text" placeholder="请输入问题编号" /></u-form-item>
					<u-form-item class="form-item" label="提交人"><u-input v-model="will.createName" type="text" placeholder="请输入提交人" /></u-form-item>
					<u-form-item class="form-item" label="代表之家">
						<u-input v-model="home" type="select" :select-open="homeShow" placeholder="请选择代表之家" @click="homeShow = true" />
						<u-select :list="homeList" v-model="homeShow" @confirm="homeConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="联络站">
						<u-input v-model="llz" type="select" :select-open="llzShow" placeholder="请选择联络站" @click="llzShow = true" />
						<u-select :list="llzList" v-model="llzShow" @confirm="llzConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="网格">
						<u-input v-model="wg" type="select" :select-open="wgShow" placeholder="请选择网格" @click="wgShow = true" />
						<u-select :list="wgList" v-model="wgShow" @confirm="wgConfirm"></u-select>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<view class="search-view"><u-search class="search-input" v-model="will.title" :show-filter="true" @search="search" @custom="show = !show"></u-search></view>
		<u-tabs class="uTabs" :list="tabsList" v-model="tabsCurrent" @change="tabsChange" :is-scroll="true"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_01" v-for="(item, index) of willList" :key="item.id" @click="navTo('/pagesB/will/detail?id=' + item.id)">
				<view class="info-title">{{ item.title || item.opinionDescribe }}</view>
				<view class="info-btm">
					<view class="btm-left clamp">
						<text class="left-name">{{ item.createName }}</text>
						<text class="left-time">{{ item.createTime }}</text>
					</view>
					<view class="btm-right">
						<view class="right-type" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusName }}</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<view class="btn-add" v-if="$sysBtns('lx_cwcy')" @click="navTo('/pagesB/will/add')">
			<u-icon name="plus" color="#fff" size="40"></u-icon>
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabIndex: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '所有',
					value: 0
				},
				{
					name: '待审查',
					value: 1
				},
				{
					name: '待交办',
					value: 2
				},
				{
					name: '办理中',
					value: 3
				},
				{
					name: '已办结',
					value: 4
				},
				{
					name: '不予受理',
					value: 5
				}
			],
			statusMap: {
				0: '所有',
				1: '待审查',
				2: '待交办',
				3: '处理中',
				4: '已办结',
				5: '不予受理'
			},
			colorMap: {
				0: '#0099ff',
				1: '#ff9900',
				2: '#19be6b',
				3: '#00ff00',
				4: '#00ffff',
				5: '#ffff00'
			},
			status: 0,
			title: '',

			home: '',
			homeShow: false,
			homeList: [],

			llz: '',
			llzShow: false,
			llzList: [],

			wg: '',
			wgShow: false,
			wgList: [],

			createTime: '',
			endTime: '',

			willList: [],

			will: {
				title: '',
				opinionNo: '',
				dataType: '1',
				phone: '',
				parentId: '',
				home: '',
				station: '',
				street: '',
				createName: '',
				nodeStatus: '0'
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.status) {
			this.status = option.status;
			this.will.nodeStatus = option.status;
			this.tabsCurrent = option.status;
		}
		if (option && option.type) {
			this.type = option.type;
			this.will.dataType = option.type;
		}
		this.loadData();
		this.loadRepHome();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.roleCodes = this.userInfo.roleCodes.join(',');

				this.pageNum = 1;
				this.pageSize = 15;
			} else {
				this.$logout();
			}
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.will.dataType = e[0].value;
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			if (this.status == 0) {
				this.will.nodeStatus = '';
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				clientCode: this.clientCode,
				roleCodes: this.roleCodes,
				...this.will
			};

			if (!this.$roleCodes('sys_manager')) {
				data.createBy = this.userId;
			}

			let url = '/minqingzhitong/msPublicOpinion/allList';

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.willList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.willList = this.willList.concat(res.data.records);
				this.loaded = true;
				if (this.willList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadRepHome() {
			this.$request('/system/sys-dept/getRepHome', {
				pageNum: 1,
				pageSize: 9999,
				clientCode: this.clientCode
			}).then((res) => {
				if (res.data) {
					// this.homeList = res.data.records;

					res.data.records.forEach((item) => {
						this.homeList.push({
							id: item.id,
							parentId: item.parentId,
							value: item.id,
							label: item.deptName
						});
					});
				}
			});
		},
		loadChildren(pId, list) {
			this.$request('/system/sys-dept/getChildren', {
				clientCode: this.clientCode,
				parentId: pId
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.records.forEach((item) => {
					this[list].push({
						id: item.id,
						parentId: item.parentId,
						value: item.id,
						label: item.deptName
					});
				});
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		homeConfirm(e) {
			this.home = e[0].label;
			this.will.home = e[0].value;
			this.will.parentId = e[0].value;

			this.llz = '';
			this.loadChildren(e[0].value, 'llzList');
		},
		llzConfirm(e) {
			this.llz = e[0].label;
			this.will.station = e[0].value;
			this.will.parentId = e[0].value;

			this.wg = '';
			this.loadChildren(e[0].value, 'wgList');
		},
		wgConfirm(e) {
			this.wg = e[0].label;
			this.will.street = e[0].value;
			this.will.parentId = e[0].value;
		},
		tab(e) {
			this.tabIndex = e;
			this.loadData('refresh');
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.tabIndex = index * 10 + 10;
			this.typeFunc(this.tabsList[index].value);
		},
		filter() {
			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.will.title = '';
			this.will.opinionNo = '';
			this.type = '';
			this.will.dataType = '1';
			this.will.createTime = '';

			this.home = '';
			this.will.home = '';
			this.will.parentId = '';
			this.llz = '';
			this.will.station = '';
			this.will.parentId = '';
			this.wg = '';
			this.will.street = '';
			this.will.parentId = '';

			this.loadData('refresh');
		},
		search(e) {
			this.will.title = e;
			this.loadData('refresh');
		},
		typeFunc(e) {
			this.status = e;
			this.will.nodeStatus = e.toString();
			this.loadData('refresh');
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style lang="scss">
.tab-box {
	padding: 18rpx 0;
	font-size: 14px;
	color: #666;
	line-height: 60rpx;
	overflow-x: auto;
	background-color: #fff;

	& + .tab-box {
		padding-top: 0;
	}

	.search-filter {
		margin: 0 30rpx;
		width: 120rpx;
		height: 60rpx;
		float: right;
		text-align: center;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.title {
		padding: 0 30rpx;
		float: left;
		font-weight: bold;
	}
}

.tab-nav {
	justify-content: space-around;
	padding: 0 20rpx;
	border: 0;
	background-color: #fff;

	&:after {
		// content: '';
		flex-shrink: 0;
		width: 20rpx;
		height: 1rpx;
	}

	.tab-item {
		flex-shrink: 0;
		margin: 0 10rpx;
		padding: 0 20rpx;
		max-width: inherit;
		width: auto;
		height: 60rpx;
		color: #666;
		text-align: center;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.tab-item.active {
		font-weight: bold;
		color: #fff;
		background-color: #d0021b;
	}
}

.list-box {
	padding: 0 30rpx;
	background-color: #fff;
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid #f5f7fa;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}

.uploader-add {
	padding: 0;
	width: 136rpx;
	height: 136rpx;
	background-color: inherit;
}
</style>
